package com.ezandroid.library.network.http;

import android.util.Log;

import com.ezandroid.library.network.http.request.Request;

import java.util.Map;

/**
 * The http request log helper class.
 *
 * @author markmjw
 * @date 2014-12-31
 */
public class HttpLogger {

    public static boolean DEBUG = false;

    /**
     * Print the request params.
     *
     * @param request
     */
    public static void printParams(Request<?> request) {
        StringBuilder builder = new StringBuilder();
        builder.append("\nHttp url : ").append(request.getUrl());

        String method;
        switch (request.getMethod()) {
            case Request.Method.GET:
                method = "GET";
                break;
            case Request.Method.DELETE:
                method = "DELETE";
                break;
            case Request.Method.POST:
                method = "POST";
                break;
            case Request.Method.PUT:
                method = "PUT";
                break;
            case Request.Method.HEAD:
                method = "HEAD";
                break;
            case Request.Method.OPTIONS:
                method = "OPTIONS";
                break;
            case Request.Method.TRACE:
                method = "TRACE";
                break;
            case Request.Method.PATCH:
                method = "PATCH";
                break;
            default:
                method = "GET";
        }

        builder.append("\nHttp method : ").append(method);

        Map<String, String> headers = request.getHeaders();
        if (null != headers && !headers.isEmpty()) {
            final StringBuilder headerBuilder = new StringBuilder();
            headerBuilder.append("\nHttp headers: ");
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                headerBuilder.append("[");
                headerBuilder.append(entry.getKey());
                headerBuilder.append(" = ");
                headerBuilder.append(entry.getValue());
                headerBuilder.append("] ");
            }
            builder.append(headerBuilder.toString());
        }

//        byte[] body = request.getBody();
//        if (null != body && body.length > 0) {
//            builder.append(new String(body, Charset.forName("UTF-8"));
//        }

        if (DEBUG) {
            Log.i("HttpLogger", builder.toString());
        }
    }

    /**
     * Print request response.
     *
     * @param statusCode
     * @param url
     * @param content
     */
    public static void printResponse(int statusCode, String url, String content, long time) {
        String builder = "\nHttp url : " + url +
                "\nHttp code : " + statusCode +
                "\nHttp time : " + time + "ms";
//                "\nHttp content : " + content;

        if (DEBUG) {
            Log.w("HttpLogger", builder);
        }
    }
}
